#include "stdafx.h"
#include <vector>
#include "FileSerialize.h"
#include "IO/FileSystem/BinaryFile.h"
#include <string>

using namespace std;
using namespace irr::io;

//FileSerialize::FileSerialize(CBinaryFile& file, bool isRead)
//:m_file(file)
//,m_isRead(isRead)
//,m_buffer(NULL)
//,m_bufferLength(-1) {
//	;
//}

//FileSerialize::FileSerialize(const char* filename, bool isRead) {
//	Init();
//
//	m_isRead = isRead;
//	if( m_isRead ) {
//		
//	}
//	else {
//
//	}
//	
//}

FileSerialize::FileSerialize(IReadFile* file) {
	Init();
	file->grab();
	m_reader = file;
	m_isRead = true;
}

FileSerialize::FileSerialize(IWriteFile* file) {
	Init();
	file->grab();
	m_writer = file;
	m_isRead = false;
}

void FileSerialize::Init() {
	m_buffer = NULL;
	m_bufferLength = -1;
	m_writer = NULL;
	m_reader = NULL;
}

template<>
irr::s32 FileSerialize::Serialize(std::wstring &value) {
	return 0;
}

irr::s32 FileSerialize::Serialize(char *value, irr::s32 &numOfValue) {
	return 0;
}

irr::s32 FileSerialize::Serialize(wchar_t *value, irr::s32 &numOfValue) {
	return 0;
}


irr::s32 FileSerialize::SerializeBuffer(char *value, irr::s32 length) {
	irr::s32 numOfSerialized = 0;
	if( m_isRead ) {
		if(length > m_bufferLength) {
			SAFE_DEL_ARRAY(m_buffer);
			m_buffer = new char[length + 1];
			m_bufferLength = length;
		}
		numOfSerialized += m_reader->read(m_buffer, length);
	}
	else {

	}

	return numOfSerialized;
}


